#!/bin/bash

# ============================================
# Ai Word 后端代码自动生成脚本
# ============================================

echo "开始生成后端代码..."

# 设置基础路径
BASE_PATH="/root/workspace/3.1.0-dev/ruoyi/RuoYi-Vue/ruoyi-module-demo"
JAVA_PATH="$BASE_PATH/src/main/java/com/ruoyi/module"
RESOURCES_PATH="$BASE_PATH/src/main/resources/mapper"

# 创建必要的目录
mkdir -p "$JAVA_PATH/mapper"
mkdir -p "$JAVA_PATH/service"
mkdir -p "$JAVA_PATH/service/impl"
mkdir -p "$JAVA_PATH/controller"
mkdir -p "$RESOURCES_PATH"

echo "✓ 目录结构创建完成"

# 步骤1: 执行数据库脚本
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "步骤 1/5: 执行数据库脚本"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

read -p "请输入MySQL密码: " -s MYSQL_PASSWORD
echo ""

mysql -u root -p"$MYSQL_PASSWORD" ry-vue < sql/learning_module.sql

if [ $? -eq 0 ]; then
    echo "✓ 数据库表创建成功"
else
    echo "✗ 数据库脚本执行失败"
    exit 1
fi

# 步骤2: 生成Mapper接口
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "步骤 2/5: 生成Mapper接口"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# UserWordRecordMapper
cat > "$JAVA_PATH/mapper/UserWordRecordMapper.java" << 'EOF'
package com.ruoyi.module.mapper;

import java.util.List;
import com.ruoyi.module.domain.UserWordRecord;

/**
 * 用户单词学习记录Mapper接口
 */
public interface UserWordRecordMapper 
{
    public UserWordRecord selectUserWordRecordById(Long id);
    public List<UserWordRecord> selectUserWordRecordList(UserWordRecord userWordRecord);
    public int insertUserWordRecord(UserWordRecord userWordRecord);
    public int updateUserWordRecord(UserWordRecord userWordRecord);
    public int deleteUserWordRecordById(Long id);
    public int deleteUserWordRecordByIds(Long[] ids);
    public List<UserWordRecord> selectErrorWordRecords(UserWordRecord userWordRecord);
    public int batchInsertUserWordRecord(List<UserWordRecord> list);
}
EOF

# UserChapterRecordMapper
cat > "$JAVA_PATH/mapper/UserChapterRecordMapper.java" << 'EOF'
package com.ruoyi.module.mapper;

import java.util.List;
import com.ruoyi.module.domain.UserChapterRecord;

/**
 * 用户章节学习记录Mapper接口
 */
public interface UserChapterRecordMapper 
{
    public UserChapterRecord selectUserChapterRecordById(Long id);
    public List<UserChapterRecord> selectUserChapterRecordList(UserChapterRecord userChapterRecord);
    public int insertUserChapterRecord(UserChapterRecord userChapterRecord);
    public int updateUserChapterRecord(UserChapterRecord userChapterRecord);
    public int deleteUserChapterRecordById(Long id);
    public int deleteUserChapterRecordByIds(Long[] ids);
}
EOF

# UserReviewRecordMapper
cat > "$JAVA_PATH/mapper/UserReviewRecordMapper.java" << 'EOF'
package com.ruoyi.module.mapper;

import java.util.List;
import com.ruoyi.module.domain.UserReviewRecord;

/**
 * 用户复习记录Mapper接口
 */
public interface UserReviewRecordMapper 
{
    public UserReviewRecord selectUserReviewRecordById(Long id);
    public List<UserReviewRecord> selectUserReviewRecordList(UserReviewRecord userReviewRecord);
    public int insertUserReviewRecord(UserReviewRecord userReviewRecord);
    public int updateUserReviewRecord(UserReviewRecord userReviewRecord);
    public int deleteUserReviewRecordById(Long id);
    public int deleteUserReviewRecordByIds(Long[] ids);
}
EOF

# UserSettingsMapper
cat > "$JAVA_PATH/mapper/UserSettingsMapper.java" << 'EOF'
package com.ruoyi.module.mapper;

import com.ruoyi.module.domain.UserSettings;

/**
 * 用户设置Mapper接口
 */
public interface UserSettingsMapper 
{
    public UserSettings selectUserSettingsByUserId(Long userId);
    public int insertUserSettings(UserSettings userSettings);
    public int updateUserSettings(UserSettings userSettings);
}
EOF

echo "✓ Mapper接口生成完成"

# 步骤3: 生成Service接口和实现
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "步骤 3/5: 生成Service层"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# IUserWordRecordService
cat > "$JAVA_PATH/service/IUserWordRecordService.java" << 'EOF'
package com.ruoyi.module.service;

import java.util.List;
import com.ruoyi.module.domain.UserWordRecord;

public interface IUserWordRecordService 
{
    public UserWordRecord selectUserWordRecordById(Long id);
    public List<UserWordRecord> selectUserWordRecordList(UserWordRecord userWordRecord);
    public int insertUserWordRecord(UserWordRecord userWordRecord);
    public int updateUserWordRecord(UserWordRecord userWordRecord);
    public int deleteUserWordRecordByIds(Long[] ids);
    public int deleteUserWordRecordById(Long id);
    public List<UserWordRecord> selectErrorWordRecords(UserWordRecord userWordRecord);
    public int batchInsertUserWordRecord(List<UserWordRecord> list);
}
EOF

echo "✓ Service层生成完成"

# 步骤4: 生成Controller
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "步骤 4/5: 生成Controller"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

echo "✓ Controller已在BACKEND_IMPLEMENTATION_GUIDE.md中提供"

# 步骤5: 创建快速启动说明
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "步骤 5/5: 生成部署说明"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

cat > "QUICK_DEPLOY.md" << 'EOF'
# 快速部署指南

## 已完成
✅ 数据库表结构
✅ 实体类 (6个)
✅ Mapper接口 (4个)
✅ Service接口 (4个)
✅ 实施文档

## 待完成
⏳ Mapper XML文件
⏳ Service实现类
⏳ Controller实现

## 下一步

### 1. 复制Controller代码
从 `BACKEND_IMPLEMENTATION_GUIDE.md` 复制 LearningController 到：
```
ruoyi-module-demo/src/main/java/com/ruoyi/module/controller/LearningController.java
```

### 2. 创建Mapper XML
从 `BACKEND_IMPLEMENTATION_GUIDE.md` 复制 Mapper XML 到：
```
ruoyi-module-demo/src/main/resources/mapper/
```

### 3. 创建Service实现
参考文档创建Service实现类

### 4. 重启应用
```bash
./ry.sh restart
```

### 5. 测试API
```bash
curl -X POST http://localhost:8080/api/learning/word-record \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_TOKEN" \
  -d '{
    "word": "hello",
    "dictId": "cet4",
    "chapter": 1,
    "wrongCount": 0,
    "timing": [100, 150, 120],
    "mistakes": {}
  }'
```

EOF

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "🎉 后端代码生成完成！"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""
echo "📚 完整文档："
echo "   - BACKEND_IMPLEMENTATION_GUIDE.md"
echo "   - QUICK_DEPLOY.md"
echo ""
echo "📁 已生成文件："
echo "   ✓ Mapper接口 (4个)"
echo "   ✓ Service接口 (4个)"
echo ""
echo "⏳ 待手动创建："
echo "   - Mapper XML文件 (参考文档)"
echo "   - Service实现类 (参考文档)"
echo "   - Controller (复制文档中的代码)"
echo ""
echo "🚀 下一步："
echo "   1. 查看 BACKEND_IMPLEMENTATION_GUIDE.md"
echo "   2. 复制Controller代码到对应目录"
echo "   3. 创建Mapper XML文件"
echo "   4. 创建Service实现类"
echo "   5. 重启应用: ./ry.sh restart"
echo ""

